<?php
class Con_task extends Acan{

	public $defaultAct='list';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
	}

	function Act_index(){
	}
	function Act_runcmd(){
		echo Afun_run_cmd($_POST['cmd']);
	}
	//tasklist
	function Act_list(){
		$data = explode("\n",Afun_run_cmd("schtasks /query /FO csv"));
		foreach ($data as $key => $val) {
			if(empty($val)){continue;}
			$val = str_replace('"','',$val);
			$ds =explode(',',$val);
			$drr = explode("\\",$ds[0]);
			if($key>0 && $ds[0]!='TaskName' && count($drr)==2){
				$ds[0] = str_replace("\\",'',$ds[0]);
				$datas->list[] = $ds;
			}elseif($key=='0'){
				$datas->head = $ds;
			}
		}
		include $this->Atpl();
	}
	function Act_view(){
		$taskname = $_GET['tn'];
		if($taskname) $cmd = "schtasks /query /v /fo list /tn \"$taskname\"";
		$data = explode("\n",Afun_run_cmd($cmd));
		foreach ($data as $key => $val) {
			$drr = explode(':',$val);
			if(count($drr)==2){
				$drr[1] = trim($drr[1]);
				$datas[] = $drr;
			}
		}
		include $this->Atpl();
	}
	function Act_run(){
		$taskname = $_GET['tn'];
		if($taskname) $cmd = "schtasks /run /tn \"$taskname\"";
		echo $msg = Afun_run_cmd($cmd);
		Afun_refresh('运行成功','referrer');
	}
	function Act_del(){
		$taskname = $_GET['tn'];
		if($taskname) $cmd = "schtasks /delete /tn \"$taskname\" /F";
		echo $msg = Afun_run_cmd($cmd);
		Afun_refresh('删除任务:'.$taskname.' 成功','referrer');
	}
	function Act_add_autorun(){
		$ini_path = Acan_anmp_path."/run/cron/autorun.cmd";
		if(file_exists($ini_path)){
			unlink($ini_path);
		}
		$prr = explode(':',Acan_anmp_path);
		$ini = '@ECHO OFF
'.$prr[0].':
cd '.Acan_anmp_path.'
SET AROOT=%CD%
cd run
call memcache_start
call phpcgi_start
call nginx_start
pause nul
';
		file_put_contents($ini_path,Afun_charset(str_replace("\n","\r\n",$ini),Acan_anmp_charset));
		if(Acan_anmp_pcpass){
			$auth = '/ru '.Acan_anmp_pcuser.' /rp '.Acan_anmp_pcpass;
		}else{
			$auth = '';
		}
		$cmd = 'schtasks /delete /tn "Anmp-autorun" /f';
		echo $msg = Afun_run_cmd($cmd);
		$cmd = 'schtasks /create /tn "Anmp-autorun" /tr "'.$ini_path.'" /sc onstart';
		echo $msg = Afun_run_cmd($cmd);
		Afun_refresh('添加任务成功','referrer');
	}
	function Act_add_nginxlogmove(){
	echo	$ini_path = Acan_anmp_path."/run/cron/nginx_log.cmd";
		if(file_exists($ini_path)){
			unlink($ini_path);
		}
		$prr = explode(':',Acan_anmp_path);
		$ini = '@ECHO OFF
'.$prr[0].':
cd '.Acan_anmp_path.'
SET AROOT=%CD%
SET Adate=%date:~0,4%-%date:~5,2%-%date:~8,2%
if exist temp\alogs (echo 存在) else (mkdir temp\alogs)
if exist temp\alogs\%Adate% (echo 存在) else (mkdir temp\alogs\%Adate%)
move temp\logs\*.log temp\alogs\%Adate%
echo 移动日志完成
cd temp
%AROOT%\server\nginx\nginx -s reload -c %AROOT%\config\nginx\nginx.conf
echo 重读nginx成功
';
		file_put_contents($ini_path,Afun_charset(str_replace("\n","\r\n",$ini),Acan_anmp_charset));
		if(Acan_anmp_pcpass){
			$auth = '/ru '.Acan_anmp_pcuser.' /rp '.Acan_anmp_pcpass;
		}else{
			$auth = '';
		}
		$cmd = 'schtasks /delete /tn "Anginx_log" /f';
		echo $msg = Afun_run_cmd($cmd);
		$cmd = 'schtasks /create /tn "Anginx_log" /tr "'.$ini_path.'" /sc daily /mo 1 /st 00:00:01 '.$auth;
		echo $msg = Afun_run_cmd($cmd);
		Afun_refresh('添加任务成功','referrer');
	}
	function Act_add_nginxlogdel(){
		$ini_path = Acan_anmp_path."/run/cron/nginx_logdel.cmd";
		if(file_exists($ini_path)){
			unlink($ini_path);
		}
		$prr = explode(':',Acan_anmp_path);
		$ini = '@ECHO OFF
'.$prr[0].':
cd '.Acan_anmp_path.'
SET AROOT=%CD%
cd run
%AROOT%\server\php\php.exe -c %AROOT%\config\php.ini "Ainstall.php" "del_nginx_log_run" ""
echo 定时删除nginx日志成功
';
		file_put_contents($ini_path,Afun_charset(str_replace("\n","\r\n",$ini),Acan_anmp_charset));
		if(Acan_anmp_pcpass){
			$auth = '/ru '.Acan_anmp_pcuser.' /rp '.Acan_anmp_pcpass;
		}else{
			$auth = '';
		}
		$cmd = 'schtasks /delete /tn "Anginx_log_del" /f';
		echo $msg = Afun_run_cmd($cmd);
		$cmd = 'schtasks /create /tn "Anginx_log_del" /tr "'.$ini_path.'" /sc daily /mo '.Acan_dellogs_run.' /st 00:10:00 '.$auth;
		echo $msg = Afun_run_cmd($cmd);
		Afun_refresh('添加任务成功','referrer');
	
	}
	
}

?>